package commEntityManager

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commLogger"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisService"
	"gitee.com/fierce_wolf/go-fox-edge-common/commSql"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Differ"
)

func (e *entityManager) InitLoadEntity() bool {
	isInitialized := true

	for key, _ := range EntityManager.SourceTypes {
		ok := e.InitLoadDB2RedisProducer(key)
		if !ok {
			commLogger.Error("初始化装载失败：" + key)
		}
		isInitialized = isInitialized && ok
	}

	producer := commRedisService.IRedisManager.GetRedisProducer()
	for k := range producer {
		isInitialized = isInitialized && commRedisService.IRedisManager.InitLoadProducerEntity(k)
	}

	consumer := commRedisService.IRedisManager.GetRedisConsumer()
	for k := range consumer {
		isInitialized = isInitialized && commRedisService.IRedisManager.InitLoadConsumerEntity(k)
	}

	return isInitialized
}

func (e *entityManager) InitLoadDB2RedisProducer(entityType string) bool {
	if !commSql.HasSchema(entityType) {
		return false
	}

	producer := commRedisService.IRedisManager.GetRedisProducer()
	_, ok := producer[entityType]
	if ok {
		producerRedisService := commRedisService.IRedisManager.GetRedisService(entityType)
		if producerRedisService == nil {
			return false
		}

		// 检查：是否已经初始化过
		if !producerRedisService.IsInited() {
			// 从数据库装载数据
			entityList, err := commSql.QueryEntityList(entityType)
			if err != nil {
				commLogger.Error("装载数据库失败：" + entityType)
				return false
			}

			// 保存到本地缓存中
			producerRedisService.Init(entityList)
			producerRedisService.SetInited()
		}

		return true
	}

	writer := commRedisService.IRedisManager.GetRedisWriter()
	_, ok = writer[entityType]
	if ok {
		// 从数据库中读取数据
		entityList, err := commSql.QueryEntityList(entityType)
		if err != nil {
			commLogger.Error("读取数据库失败：" + entityType + " 错误信息：" + err.Error())
			commSql.QueryEntityList(entityType)
			return false
		}

		// 转成MAP
		dataMap := make(map[string]commEntity.IEntity)
		for _, entity := range entityList {
			dataMap[entity.MakeServiceKey()] = entity
		}

		// 写入数据到redis
		redisOperator := commRedisService.IRedisManager.GetOperatorWriter(entityType)
		redisOperator.WriteEntityMap(dataMap)

		// 重新读取redis中的数据
		redisMap, err := redisOperator.ReadAllDataMap()
		if err != nil {
			commLogger.Error("读取Redis失败：" + entityType + " 错误信息：" + err.Error())
			return false
		}

		// 比较差异
		addList := make(map[string]interface{})
		delList := make(map[string]interface{})
		Differ.Set.DifferByKey(redisMap, dataMap, &addList, &delList)

		// 删除redis中的垃圾数据
		for key, _ := range delList {
			redisOperator.DeleteEntity(key)
		}

		return true
	}

	return false
}

func (e *entityManager) SyncEntity() {
	// 检查：是否已经初始化完成
	if !e.Initialized {
		return
	}

	// 获得类型列表
	types := make(map[string]interface{})
	consumer := commRedisService.IRedisManager.GetRedisConsumer()
	producer := commRedisService.IRedisManager.GetRedisProducer()
	for k, v := range producer {
		types[k] = v
	}
	for k, v := range consumer {
		types[k] = v
	}

	for k := range types {
		e.syncEntityByType(k)
	}
}

func (e *entityManager) syncEntityByType(entityType string) {
	// 检查：是否已经初始化完成
	if !e.Initialized {
		return
	}

	producer := commRedisService.IRedisManager.GetRedisProducer()
	_, ok := producer[entityType]
	if ok {
		commRedisService.IRedisManager.UpdateRedisService(entityType)
	}

	consumer := commRedisService.IRedisManager.GetRedisConsumer()
	_, ok = consumer[entityType]
	if ok {
		commRedisService.IRedisManager.ReloadRedisService(entityType)
	}
}
